import time

from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)

driver.get("https://demoqa.com/selectable")

GRID_TAB_LOCATOR = ("xpath", "//a[@id='demo-tab-grid']")
GRID_CONTAINER_LOCATOR = ("css selector", ".grid-container.mt-4")
ROW_LOCATOR = ("css selector", ".list-group.list-group-horizontal-sm")
ITEM_LOCATOR = ("css selector", ".list-group-item.list-group-item-action")

# Открыть вкладку Grid
grid_tab = driver.find_element(*GRID_TAB_LOCATOR)
grid_tab.click()
time.sleep(3)

grid_container = driver.find_element(*GRID_CONTAINER_LOCATOR)
rows = grid_container.find_elements(*ROW_LOCATOR)

# # Кликнуть на пару любых элементов
# for row in rows:
#     items = row.find_elements(*ITEM_LOCATOR)
#     for item in items:
#         time.sleep(1)
#         item.click()
#
# # Убедиться, что они выбраны
# for row in rows:
#     items = row.find_elements(*ITEM_LOCATOR)
#     for item in items:
#         assert "active" in item.get_attribute("class")
#
# # Кликнуть еще раз и убедиться, что теперь они не выбраны
# for row in rows:
#     items = row.find_elements(*ITEM_LOCATOR)
#     for item in items:
#         item.click()
#         assert "active" not in item.get_attribute("class")

items = grid_container.find_elements(*ITEM_LOCATOR)
items[0].click()
items[4].click()
time.sleep(3)

for item in items[0:5:4]:
    assert "active" in item.get_attribute("class")

for item in items[0:5:4]:
    item.click()
    assert "active" not in item.get_attribute("class")

